package com.coursehub.course.repository;

import com.coursehub.course.entity.Course;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Optional;

/**
 * 课程数据访问层
 */
@Repository
public interface CourseRepository extends JpaRepository<Course, String>, JpaSpecificationExecutor<Course> {
    
    /**
     * 根据课程名称查找课程
     */
    Optional<Course> findByCourseName(String courseName);
    
    /**
     * 根据课程名称模糊查询
     */
    List<Course> findByCourseNameContaining(String courseName);
    
    /**
     * 检查课程名称是否存在
     */
    boolean existsByCourseName(String courseName);
    
    /**
     * 根据课程ID检查课程是否存在
     */
    boolean existsByCourseId(String courseId);
    
    /**
     * 根据考核方式查找课程
     */
    List<Course> findByExaminationMethod(String examinationMethod);
    
    /**
     * 自定义查询：根据多个条件查找课程
     */
    @Query("SELECT c FROM Course c WHERE " +
           "(:courseName IS NULL OR c.courseName LIKE %:courseName%) AND " +
           "(:examinationMethod IS NULL OR c.examinationMethod = :examinationMethod)")
    List<Course> findCoursesByConditions(@Param("courseName") String courseName, 
                                       @Param("examinationMethod") String examinationMethod);
}